package com.example.mybaseapp.util

import android.content.Context
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CameraManager
import android.os.Build
import android.util.Log


/**
 * 逻辑摄像头 工具类
 */
class CameraUtil(context:Context) {
    private var cameraManager: CameraManager = context.getSystemService(CameraManager::class.java)

    /**
     * Helper class used to encapsulate a logical camera and two underlying
     * physical cameras
     */
    data class DualCamera(val logicalId: String, val physicalId1: String, val physicalId2: String)

//    fun findDualCameras(manager: CameraManager, facing: Int? = null): List

    fun findLogicCameraId(){
        val cameraIds: Array<String> = cameraManager.cameraIdList
        val backCameraIds: MutableList<String> = ArrayList()
        for (id in cameraIds) {
            val cameraCharacteristics: CameraCharacteristics =
                cameraManager.getCameraCharacteristics(id)
            if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) != CameraCharacteristics.LENS_FACING_BACK) continue

            backCameraIds.add(id)
        }
        Log.d("Fire", "findLogicCameraId: ${backCameraIds.size}")

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            val logicCameraIds: MutableList<String> = ArrayList()
            for (id in backCameraIds) {
                val cameraCharacteristics: CameraCharacteristics =
                    cameraManager.getCameraCharacteristics(id)
                val available =
                    cameraCharacteristics.get(CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES)
                for (i in available!!) {
                    if (i == CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA) {
                        Log.d("Fire", "findLogicCameraId: 逻辑摄像头：$id")
                        logicCameraIds.add(id)
                    }
                }
            }
        }
    }

//    fun findShortLongCameraPair(manager: CameraManager, facing: Int? = null): DualCamera? {
//
//        return findDualCameras(manager, facing).map {
//            val characteristics1 = manager.getCameraCharacteristics(it.physicalId1)
//            val characteristics2 = manager.getCameraCharacteristics(it.physicalId2)
//
//            // Query the focal lengths advertised by each physical camera
//            val focalLengths1 = characteristics1.get(
//                CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(0F)
//            val focalLengths2 = characteristics2.get(
//                CameraCharacteristics.LENS_INFO_AVAILABLE_FOCAL_LENGTHS) ?: floatArrayOf(0F)
//
//            // Compute the largest difference between min and max focal lengths between cameras
//            val focalLengthsDiff1 = focalLengths2.maxOrNull()!! - focalLengths1.minOrNull()!!
//            val focalLengthsDiff2 = focalLengths1.maxOrNull()!! - focalLengths2.minOrNull()!!
//
//            // Return the pair of camera IDs and the difference between min and max focal lengths
//            if (focalLengthsDiff1 < focalLengthsDiff2) {
//                Pair(DualCamera(it.logicalId, it.physicalId1, it.physicalId2), focalLengthsDiff1)
//            } else {
//                Pair(DualCamera(it.logicalId, it.physicalId2, it.physicalId1), focalLengthsDiff2)
//            }
//
//            // Return only the pair with the largest difference, or null if no pairs are found
//        }.maxByOrNull { it.second }?.first
//    }
//
//    fun openDualCamera(cameraManager: CameraManager,
//                       dualCamera: DualCamera,
//        // AsyncTask is deprecated beginning API 30
//                       executor: Executor = AsyncTask.SERIAL_EXECUTOR,
//                       callback: (CameraDevice) -> Unit) {
//
//        // openCamera() requires API >= 28
//        cameraManager.openCamera(
//            dualCamera.logicalId, executor, object : CameraDevice.StateCallback() {
//                override fun onOpened(device: CameraDevice) = callback(device)
//                // Omitting for brevity...
//                override fun onError(device: CameraDevice, error: Int) = onDisconnected(device)
//                override fun onDisconnected(device: CameraDevice) = device.close()
//            })
//    }
}